using System;
using System.Text;
using System.Runtime.InteropServices;

using Canlib;


public class Hello1
{

   public static void Main()
   {
      String s = "";
      int h, h2;
      CanlibAPI.canStatus stat;

      System.Console.WriteLine("Hello, CANLIB!");
      CanlibAPI.canInitializeLibrary();
      
      h = CanlibAPI.canOpenChannel(0, CanlibAPI.canOPEN_ACCEPT_VIRTUAL);
      h2 = CanlibAPI.canOpenChannel(1, CanlibAPI.canOPEN_ACCEPT_VIRTUAL);

      stat = CanlibAPI.canBusOn(h);
      System.Console.WriteLine("canBusOn 0: stat={0}", stat);
      stat = CanlibAPI.canBusOn(h2);
      System.Console.WriteLine("canBusOn 1: stat={0}", stat);

      
      // Test canGetErrorText()
      CanlibAPI.canGetErrorText(CanlibAPI.canStatus.canERR_PARAM, ref s);
      System.Console.WriteLine(s);

      // Test canIoCtl()
      uint buflevel = 77676;
      stat = CanlibAPI.canIoCtl(h, CanlibAPI.canIOCTL_GET_RX_BUFFER_LEVEL, ref buflevel);
      System.Console.WriteLine("Stat={1}, Rx Buflevel={0}", buflevel, stat);

      // Test canWrite()
      byte[] msg = new byte[6];
      stat = CanlibAPI.canWrite(h, 123, msg, 6, 0);
      System.Console.WriteLine("canWrite: stat={0}", stat);

      s = System.Console.ReadLine();

      // Test canIoCtl()
      buflevel = 77676;
      stat = CanlibAPI.canIoCtl(h2, CanlibAPI.canIOCTL_GET_RX_BUFFER_LEVEL, ref buflevel);
      System.Console.WriteLine("Stat={1}, Rx Buflevel={0}", buflevel, stat);

      int id;
      uint dlc, flag, time;
      stat = CanlibAPI.canRead(h2, out id, msg, out dlc, out flag, out time);
      System.Console.WriteLine("canRead: stat={0}, id={1}, dlc={2}, flag={3}, time={4}",
                               stat, id, dlc, flag, time);

      
      System.Console.WriteLine("Goodbye, CANLIB!");
   }
}
